package com.itheima.service.db.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.db.BlackList;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.mapper.BlackListMapper;
import com.itheima.mapper.UserInfoMapper;
import com.itheima.service.db.BlackListService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class BlackListServiceImpl implements BlackListService {

    @Autowired
    private BlackListMapper blackListMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;
    @Override
    public PageBeanVo findByPage(Integer pageNum, Integer pageSize, Long userId) {
        //1.封装分页对象
        IPage<BlackList> page = new Page(pageNum  , pageSize);

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id" , userId);
        //2.查询数据
        page = blackListMapper.selectPage(page, queryWrapper);

        //3.拿到黑名单数据
        List<BlackList> blackList = page.getRecords();

        List<UserInfo> userInfoList = new ArrayList<>();
        //4.判断加遍历
        if(CollectionUtil.isNotEmpty(blackList)){
            for (BlackList o : blackList) {
                //黑名单用户的id -> 查询出黑名单的用户数据
                Long blackUserId = o.getBlackUserId();
                //查询黑名单用户
                UserInfo userInfo = userInfoMapper.selectById(blackUserId);
                //放入集合
                userInfoList.add(userInfo);
            }
        }
        //直接构造返回对象
        return new  PageBeanVo(pageNum , pageSize , page.getTotal() , userInfoList);
    }

    /**
     * 根据当前登录的用户id 和 黑名单用户的id 删除
     * @param loginUserId
     * @param blackUserId
     */
    @Override
    public void delete(Long loginUserId, Long blackUserId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id" ,loginUserId );
        queryWrapper.eq("black_user_id" ,blackUserId );
        blackListMapper.delete(queryWrapper);
    }
}
